package org.apache.dolphinscheduler.api.controller;

import org.apache.dolphinscheduler.api.aspect.PermissionCheck;
import org.apache.dolphinscheduler.api.service.ChargingRuleService;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.dao.entity.ChargingRule;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 收费规则表 前端控制器
 * </p>
 *
 * @author denggh
 * @since 2023-08-15
 */
@Api(tags = "收费规则")
@RestController
@RequestMapping("/chargingRule")
public class ChargingRuleController extends BaseController {

    @Autowired
    private ChargingRuleService chargingRuleService;

    /**
     * 获取收费规则列表
     */
    @GetMapping("/list")
    @ApiOperation("获取收费规则列表")
    @PermissionCheck(permission = "operation:config:rules")
    public Result list() {
        return chargingRuleService.selectChargingRuleList();
    }

    /**
     * 根据收费规则id获取详细信息
     *
     * @param chargingRuleId 收费规则id
     */
    @GetMapping(value = "/{chargingRuleId}")
    @ApiOperation("根据收费规则id获取详细信息")
    @PermissionCheck(permission = "operation:config:rules")
    public Result getInfo(@PathVariable Long chargingRuleId) {
        Map<String, Object> result = chargingRuleService.selectChargingRuleById(chargingRuleId);
        return returnDataList(result);
    }

    /**
     * 新增收费规则
     */
    @PostMapping("/add")
    @ApiOperation("新增收费规则")
    @PermissionCheck(permission = "operation:config:rules:standard,operation:config:rules:gradients")
    public Result add(@Validated @RequestBody ChargingRule chargingRule) {

        Map<String, Object> result = chargingRuleService.insertChargingRule(chargingRule);
        return returnDataList(result);
    }

    /**
     * 修改收费规则
     */
    @PostMapping("/update")
    @ApiOperation("修改收费规则")
    @PermissionCheck(permission = "operation:config:rules:standard,operation:config:rules:gradients")
    public Result edit(@Validated @RequestBody List<ChargingRule> chargingRule) {
        Map<String, Object> result = chargingRuleService.updateChargingRule(chargingRule);
        return returnDataList(result);
    }

    /**
     * 删除收费规则
     *
     * @param chargingRuleId 收费规则ID
     */
    @DeleteMapping("/{chargingRuleId}")
    @ApiOperation("删除收费规则")
    public Result remove(@PathVariable Long chargingRuleId) {
        Map<String, Object> result = chargingRuleService.deleteChargingRuleById(chargingRuleId);
        return returnDataList(result);
    }
}
